#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Math;

#endregion

namespace Weazel.Gezel.Simulator.Reference
{
  class InputSignal : AbstractSignal
  {
    private AbstractSignal driver;
    private Integer value;

    public InputSignal(Symid id, int wlen, int sign, string name) : base(id, name)
    {
      driver = null;
      value = new Integer(wlen, sign == 1);
    }

    internal override uint Width()
    {
      return (uint) value.Width;
    }

    internal override uint Signed()
    {
      return value.Signed ? (uint) 1 : (uint) 0;
    }

    internal override void defineDriver(AbstractSignal p)
    {
      driver = p;
    }

    internal override Integer Evaluate(AbstractIpBlock ip, long g)
    {
      if (driver != null)
      {
        generation = g;
        value = driver.Evaluate(ip, g);
        return value;
      }
      else
        throw new ApplicationException("error signal '" + name + "' has no driver");
    }

    internal override Integer PeekValue()
    {      
      return value;
    }

    public override bool IsGeneration(long g)
    {
      return generation == g;
    }

    public override void Show(StringBuilder buffer)
    {
      buffer.Append(value.ToString(@base));
    }

    public override bool TraceToSignal(Symid toSignalId, List<AbstractSignal> signals)
    {
      signals.Add(this);
      return driver.TraceToSignal(toSignalId, signals);
    }
  }
}